//
//  HandWritingInputView.m
//  etionUI
//
//  Created by wangjian on 12/9/13.
//  Copyright (c) 2013 GuangZhouXuanWu. All rights reserved.
//

#import "HandWritingInputView.h"

@implementation CHandWritingInputView
{
    NSTimer *_timer;
    NSUInteger _timeCount;
    BOOL shouldStartInput;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        
        _drawingTool = [[CDrawingToolPen alloc] init];
        _drawingTool.lineWidth = 4;
        _drawingTool.m_toolColor = [UIColor colorWithHex:0x3498DB];
    }
    return self;
}

- (void)dealloc
{
    [_drawingTool release];
    [_timer release];
    [super dealloc];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [self.drawingTool draw];
}

- (void)timeCount
{
    _timeCount++;
    if (_timeCount == 10)
    {
        [_timer invalidate];
        _timer = nil;
        [self confirmInput];
    }
}

- (void)confirmInput
{
    // store the image
    UIGraphicsBeginImageContext(self.bounds.size);
    [self.drawingTool draw];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    CGRect pathBound = CGRectInset(self.drawingTool.bounds, -self.drawingTool.lineWidth, -self.drawingTool.lineWidth) ;
//    CGSize wordSize = CGSizeMake(pathBound.size.width + self.drawingTool.lineWidth, pathBound.size.height + self.drawingTool.lineWidth);
//    CGFloat wordSize = MAX(pathBound.size.width, pathBound.size.height)+self.drawingTool.lineWidth;
//    if (wordSize == 0)
//    {
//        wordSize = 2*self.drawingTool.lineWidth;
//    }
    UIGraphicsBeginImageContext(pathBound.size);
    [image drawAtPoint:CGPointMake(-pathBound.origin.x, -pathBound.origin.y)];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    [self.drawingTool removeAllPoints];
    [self.delegate handWritingInputView:self inputWord:image inFrame:pathBound];
    [self setNeedsDisplay];
}

- (void)cancelInput
{
    if ([_timer isValid])
    {
        [_timer invalidate];
        _timer = nil;
    }
}

#pragma mark Touch Methods

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    shouldStartInput = YES;
    if ([self.delegate respondsToSelector:@selector(handWritingInputViewShouldStartInput:)])
    {
        shouldStartInput = [self.delegate handWritingInputViewShouldStartInput:self];
    }
    
    if (shouldStartInput == YES)
    {
        [_timer invalidate];
        _timer = nil;
        UITouch *touch = [touches anyObject];
        [self.drawingTool moveToPoint:[touch locationInView:self]];
        if (self.drawingTool.isEmpty && [self.delegate respondsToSelector:@selector(handWritingInputViewDidStartInput:)])
        {
            [self.delegate handWritingInputViewDidStartInput:self];
        }
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (shouldStartInput == YES)
    {
        UITouch *touch = [touches anyObject];
        [self.drawingTool moveFromPoint:[touch previousLocationInView:self] toPoint:[touch locationInView:self]];
        [self setNeedsDisplay];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (shouldStartInput == YES)
    {
        [self touchesMoved:touches withEvent:event];
        _timeCount = 0;
        _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timeCount) userInfo:nil repeats:YES];
        [_timer fire];
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self touchesEnded:touches withEvent:event];
}

@end
